package com.dean.service;

import cn.hutool.core.util.StrUtil;
import com.dean.constants.DeanUiConstants;
import com.dean.dao.DeanToolDao;
import com.dean.entity.DataQueryEntity;
import com.dean.entity.SysDbConnInfo;
import com.dean.exception.DeanRuntimeException;
import com.dean.sql.factory.DeanAutoSqlFactory;
import com.dean.utils.*;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName EntityGenerateService
 * @Description 实体类生成工具类
 * @Author Administrator
 * @Date 2021/10/2313:36
 */
public class DbQueryService {

    private int querySize = 1000;
    private DbQueryService(){
    }
    private static volatile DbQueryService INSTANCE;
    public static DbQueryService getInstance(){
        if(INSTANCE == null){
            synchronized (DbQueryService.class){
                if(INSTANCE == null){
                    INSTANCE = new DbQueryService();
                }
            }
        }
        return INSTANCE;
    }


    /**
     * 执行数据库查询
     * @param connInfo
     * @param sql
     */
    public DataQueryEntity query(SysDbConnInfo connInfo,String sql,Integer pageSize){
        if(StrUtil.isBlank(sql)){
            DeanRuntimeException.throwException(2001,"校验错误,参数[sql]不能为空");
        }
        // 单次默认最大返回记录数为1000 条，如有需要请自行分页
        connInfo = DeanToolDao.getInstance().queryConnInfo(connInfo.getConnName());
        String exeSql = DeanAutoSqlFactory.getDeanSql(connInfo.getUrl()).getPageSql(sql);
        DataQueryEntity queryEntity = DeanJdbcUtils.queryDataAndHead(connInfo,exeSql,pageSize);
        return queryEntity;
    }

    /**
     * 执行数据库查询
     * @param connInfo
     * @param sql
     */
    public DataQueryEntity query(SysDbConnInfo connInfo,String sql){
        return query(connInfo,sql,querySize);
    }

    /**
     * 导出文件
     * @param queryEntity
     * @param filePath
     * @param fileType
     * @return
     */
    public String exportFile(DataQueryEntity queryEntity,String filePath,String fileType) {
        String fileName = filePath+File.separator+ UUID.randomUUID()+".";
        if(DeanUiConstants.EXCEL.equals(fileType)){
            fileName += "xlsx";
            DataUtils<String> dataUtils = new DataUtils<String>();
            List<List<String>> head = dataUtils.arrayToLList(queryEntity.getHeader());
            List<List<String>> data = dataUtils.arrayToList(queryEntity.getDatas());
            DeanEasyExcelUtils.writeExcel(fileName,head,data);
        }else if(DeanUiConstants.CSV.equals(fileType)){
            fileName += "csv";
            DeanCsvUtils.writeCsv(queryEntity,fileName);
        }else if(DeanUiConstants.INSERT_SQL.equals(fileType)){
            fileName += "sql";
            String msg = TxtWriteUtils.writeTxt(fileName,queryEntity.getHeader(),queryEntity.getDatas());
            if(!"OK".equals(msg)){
                throw DeanRuntimeException.throwException(3002,msg);
            }
        }else{
            fileName = null;
        }
        return fileName;
    }

}
